package com.gkd.dao;

import com.github.pagehelper.PageInfo;
import com.gkd.mapper.DepartmentMapper;
import com.gkd.mapper.EmployeeMapper;
import com.gkd.pojo.Department;
import com.gkd.pojo.Employee;
import com.gkd.service.DepartmentService;
import com.gkd.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

@Repository
public class EmployeeDao {
    @Autowired
    DepartmentService departmentService;
    @Autowired
    EmployeeService employeeService;

    private static Map<Integer, Employee> employees = null;

    @Autowired
    private DepartmentDao departmentDao;

    public final Map<Integer,Employee> fem = new HashMap<>();
    public List<Integer> eid = new ArrayList<>();
//    static {
//        employees = new HashMap<Integer,Employee>();
//
//        employees.put(1,new Employee(1,"AA","110@qq.com",1,new Department(1,"教学部"),new Date()));
//        employees.put(2,new Employee(2,"BB","110@qq.com",0,new Department(2,"市场部"),new Date()));
//        employees.put(3,new Employee(3,"CC","110@qq.com",1,new Department(3,"教研部"),new Date()));
//        employees.put(4,new Employee(4,"DD","110@qq.com",0,new Department(4,"运营部"),new Date()));
//        employees.put(5,new Employee(5,"EE","110@qq.com",1,new Department(5,"后勤部"),new Date()));
//
//    }
    public List<Integer> queryEmployeeAllId(){
        eid = employeeService.queryEmployeeAllID();
        return eid;
    }

    public Integer sumAllIdNum(){
        return eid.size();
    }


    public Map<Integer,Employee> EmployeeMap(){
        List<Employee> employees = employeeService.queryEmployeeAll();
//        Map<Integer,Employee> em = new HashMap<>();
        for(int i=0;i<employees.size();i++){
            System.out.println(employees.get(i).getBirth());
            employees.get(i).setDepartment(departmentDao.departmentMap().get(employees.get(i).getDe_id()));
            fem.put(employees.get(i).getId(),employees.get(i));
        }
        return fem;
    }

    //            LocalDateTime birthLocalDateTime = (LocalDateTime) employees.get(i).getBirth();
//            Date birthDate = Date.from(birthLocalDateTime.atZone(ZoneId.systemDefault()).toInstant());
//            employees.get(i).setBirth(birthDate);

    private static Integer ininId=6;

    public void save(Employee employee){
        employeeService.addEmployee(employee);
        EmployeeMap();
//        if(employee.getId()==null){
//            employee.setId(ininId++);
//        }
//        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
//        employees.put(employee.getId(),employee);
    }
    public Collection<Employee> getAll(){
        return fem.values();
    }

//    public Collection<Employee> getAll(){
//        return employees.values();
//    }

    public  Employee getEmployeeById(Integer id){
        return  fem.get(id);
    }

    public void delete (Integer id){
        employeeService.removeEmployeeById(id);
        EmployeeMap();
//        EmployeeMap().remove(id);
    }

//    public PageInfo<Employee> pageInfo () {
//        return employeeService.queryAllPages(1,4);
//    }

    public void update (Employee employee){
        employeeService.updateEmployee(employee);
        EmployeeMap();
    }

    public Map<Integer,Boolean> reback(){
        Map<Integer,Boolean> cbm =employeeService.checklistById();
        return cbm;
    }

    private Map<Integer,Boolean> saverbm = new HashMap<>();
    public void setCheckListByIdMap(Map<Integer,Boolean> m){
//        Map<Integer,Boolean> rbm = m;
        this.saverbm = m;
    }

    public Map<Integer,Boolean> getCheckListByIdMap(){
        return this.saverbm;
    }

    public List<List<Object>> printALLEmployees(){
        List<Employee> saomiaoemployees = employeeService.queryEmployeeAll();
        List<List<Object>> los = new ArrayList<>();
//        List<Object> lo = new ArrayList<>();
        for (int i =0;i<saomiaoemployees.size();i++){
            List<Object> lo = new ArrayList<>();
//            System.out.println(employees.get(i).getEmail());
            lo.add(saomiaoemployees.get(i).getId());
            lo.add(saomiaoemployees.get(i).getLastName());
            lo.add(saomiaoemployees.get(i).getEmail());
            lo.add(saomiaoemployees.get(i).getGender());
            lo.add(saomiaoemployees.get(i).getBirth());
            lo.add(saomiaoemployees.get(i).getDe_id());
            lo.add(saomiaoemployees.get(i).getDepartment());
            los.add(lo);
//            lo.clear();
        }
        System.out.println("los=>"+los);
        return los;
    }

    public List<List<Object>> printSelectEmployees(){
        List<Employee> saomiaoemployees = employeeService.queryEmployeeAll();
        List<List<Object>> los = new ArrayList<>();
//        List<Object> lo = new ArrayList<>();
        for (int i =0;i<saomiaoemployees.size();i++){
            if(saverbm.get(saomiaoemployees.get(i).getId())==true){
                saomiaoemployees.get(i).setDepartment(departmentDao.getDepartmentById(saomiaoemployees.get(i).getDe_id()));
                List<Object> lo = new ArrayList<>();
                lo.add(saomiaoemployees.get(i).getId());
                lo.add(saomiaoemployees.get(i).getLastName());
                lo.add(saomiaoemployees.get(i).getEmail());
                lo.add(saomiaoemployees.get(i).getGender());
                lo.add(saomiaoemployees.get(i).getDepartment().getDepartmentName());
                lo.add(saomiaoemployees.get(i).getBirth());
//                lo.add(saomiaoemployees.get(i).getDe_id());
                los.add(lo);
            }
        }
        System.out.println("los=>"+los);
        return los;
    }
}
